package cn.etgps.etgps.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.Bind;
import butterknife.OnClick;
import cn.etgps.etgps.R;
import cn.etgps.etgps.base.MyBasicActivity;
import cn.etgps.etgps.event.EventBusSendFactory;
import cn.etgps.etgps.event.EventReceiveEntity;
import cn.etgps.etgps.utils.AppUtils;
import cn.etgps.etgps.utils.CommonUtils;
import cn.etgps.etgps.utils.GetTimeUtils;
import cn.etgps.etgps.utils.T;

/**
 * Created by itte on 2016/3/28.
 * 收车记录
 */
public class TrafficReceiveActivity extends MyBasicActivity {
    private final static String RECEIVE_ENTITY = "receive_entity";
    @Bind(R.id.tv_year)
    TextView mYear;
    @Bind(R.id.tv_time)
    TextView mTime;
    @Bind(R.id.receive_record_address)
    EditText editAddress;
    @Bind(R.id.receive_record_kim)
    EditText editKim;
    GetTimeUtils utils;
    private EventReceiveEntity receiveEntity;
    private Date time;
    private String dateStr, kmilStr, addressStr;
    /**
     * 开始时间date类型
     */
    private Date mStartDate = null;
    /**
     * 系统时间Canlendar类型
     */
    private Calendar mSystemCalendar = null;

    public static void gotoTrafficReceiveActivity(MyBasicActivity context, EventReceiveEntity receiveEntity) {
        Intent intent = new Intent(context, TrafficReceiveActivity.class);
        intent.putExtra(RECEIVE_ENTITY, receiveEntity);
        context.startActivity(intent);
    }

    @Override
    protected void setTitle() {
        titleBar.setLeftImageResource(R.mipmap.nav_back);
        titleBar.setLeftText(" ");
        titleBar.setTitle(getResources().getString(R.string.receive_record));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_receive_record;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        if (getIntent() != null) {
            if (getIntent().hasExtra(RECEIVE_ENTITY)) {
                this.receiveEntity = (EventReceiveEntity) getIntent().getSerializableExtra(RECEIVE_ENTITY);
                if (this.receiveEntity != null) {
                    bindView(receiveEntity);
                }
            }
        }
        utils = new GetTimeUtils(time, this);
    }

    private void bindView(EventReceiveEntity receiveEntity) {
        editAddress.setText(receiveEntity.getAddress());
        editKim.setText(receiveEntity.getKmil());
        mYear.setText(new SimpleDateFormat("yyyy-MM-dd").format(AppUtils.getDate(receiveEntity.getDate())));
        mTime.setText(new SimpleDateFormat("HH:mm").format(AppUtils.getDate(receiveEntity.getDate())));
        dateStr = receiveEntity.getDate();
    }

    @OnClick({R.id.receive_btn_sure, R.id.tv_time, R.id.tv_year})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.receive_btn_sure:
                if (checkOk()) {
                    EventBusSendFactory.receiveEvent(new EventReceiveEntity(
                            addressStr,
                            dateStr,
                            kmilStr
                    ));
                    finish();
                }
                break;
            case R.id.tv_time:
                if (dateStr != null) {
                    utils.getHourMintue(mTime, AppUtils.getDate(dateStr));
                } else {
                    utils.getHourMintue(mTime, null);
                }
                break;
            case R.id.tv_year:
                if (dateStr != null) {
                    utils.getYearMonthDay(mYear, "log", AppUtils.getDate(dateStr));
                } else {
                    utils.getYearMonthDay(mYear, "log", null);
                }

                break;
        }

    }

    private boolean checkOk() {

        mTime.getText().toString().trim();
        mYear.getText().toString().trim();
        kmilStr = editKim.getText().toString().trim();
        addressStr = editAddress.getText().toString().trim();
        if (utils.getDate() != null) {
            dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(utils.getDate());
        }
        if (CommonUtils.isEmpty(addressStr)) {
            T.showShort(mContext, "请输入收车地址");
            return false;
        }
        if (CommonUtils.isEmpty(dateStr) || "请选择日期".equals(mYear) || "请选择时间".equals(mTime)) {
            T.showShort(mContext, "请选择收车时间");
            return false;
        }
        if (CommonUtils.isEmpty(kmilStr)) {
            T.showShort(mContext, "请输入收车里程/km");
            return false;
        }
        return true;
    }
}
